package com.isias.system.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.isias.common.result.Result;
import com.isias.common.util.JwtHelper;
import com.isias.model.dto.HomeDto;
import com.isias.model.system.SysUser;
import com.isias.system.Service.IAuthenticationFacade;
import com.isias.system.service.CourseService;
import com.isias.system.service.HomeService;
import com.isias.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;


@RestController
@RequestMapping("/admin/home")
public class HomeController {

    @Autowired
    private IAuthenticationFacade authenticationFacade; //获取当前用户对象类

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private HomeService homeService;


    @GetMapping
    public Result GetInfo(HttpServletRequest request){
//        Authentication authentication = authenticationFacade.getAuthentication();
//        String username = authentication.getName();
        //获取请求头token字符串
        String token = request.getHeader("token");
        //从token中获取用户名称(id)
        //根据用户名获取用户信息（基本信息、部门名称，岗位名称等)
        HomeDto homeDto = homeService.getInfo(JwtHelper.getUsername(token));

        if ("".equals(homeDto.getSysUser().getId())){
            return Result.fail();
        }
        return Result.ok(homeDto);
    }


    /**
     * 获取今天的登陆人数
     * @return
     */
    @GetMapping("getDateLogin")
    public Result getDateLogin(){
        return Result.ok(homeService.getDateLogin());
    }

    /**
     * 获取我今天登陆的次数
     * @return
     */
    @GetMapping("getDateLoginByUser")
    public Result getDateLoginByUser(){
        Authentication authentication = authenticationFacade.getAuthentication();
        String username = authentication.getName();
        return Result.ok(homeService.getDateLoginByUser(username));
    }

}
